package com.macrounion.atv.processor;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macrounion.atv.cache.VirtualChannelCache;
import com.macrounion.atv.service.dto.req.VirtualReqDto;
import com.macrounion.atv.service.entity.Channel;
import com.macrounion.atv.service.entity.ext.VirtualExt;
import com.macrounion.atv.service.service.ChannelService;
import com.macrounion.atv.service.service.DeviceService;
import com.macrounion.base.service.dto.PageRespDto;
import com.macrounion.base.service.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.macrounion.atv.service.entity.VirtualChannel;
import com.macrounion.atv.service.service.VirtualChannelService;
import com.macrounion.base.processor.BaseProcessor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class VirtualChannelProcessor extends BaseProcessor<VirtualChannel, VirtualChannel, VirtualChannelService, VirtualChannel> {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private VirtualChannelCache virtualChannelCache;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ChannelProcessor channelProcessor;

    public PageRespDto<VirtualExt> requestPageList(VirtualReqDto virtualReqDto){
        PageRespDto<VirtualExt> pageResp = new PageRespDto();
        PageHelper.startPage(virtualReqDto.getPageInfo().getPageIndex(),virtualReqDto.getPageInfo().getPageSize());
        Page<VirtualExt> pm=(Page<VirtualExt>) service.getVirtualList(virtualReqDto);
        BeanUtils.copyProperties(pm, pageResp);
        pageResp.setDataList(pm.getResult());
        return pageResp;
    }

    @Transactional
    @Override
    public VirtualChannel save(VirtualChannel reqDto) {
        //检查同一设备下，通道号不能重复
        int virtualCount=findVirtualCount(reqDto.getDeviceId(),reqDto.getId(),reqDto.getChannelCode());
        int channelCount=channelService.findCount(reqDto.getDeviceId(),reqDto.getChannelCode());

        if(virtualCount>0 || channelCount>0)
            throw new BusinessException(201,"通道号已经存在");

        VirtualChannel v=super.save(reqDto);
        //channel表添加一个虚拟通道
        Channel virtualChannel=channelService.getVirtualChannel(v.getId());
        if(virtualChannel==null){
            Channel virChannel=new Channel();
            virChannel.setHistoryData(1);
            virChannel.setAlarmData(1);
            BeanUtils.copyProperties(v,virChannel,new String[] { "id", "createdTime"});
            virChannel.setVirtualId(v.getId());
            channelProcessor.save(virChannel);
        }else{
            //修改channel中的虚拟通道
            BeanUtils.copyProperties(v,virtualChannel,new String[] { "id", "createdTime","virtualId"});
            virtualChannel.setHistoryData(1);
            virtualChannel.setAlarmData(1);
            channelProcessor.save(virtualChannel);
        }

        saveVirtualCache(v.getFx());

        return v;
    }

    public int findVirtualCount(String deviceId,String virtualId,String channelCode){
        return service.findCount(deviceId,virtualId,channelCode);
    }

    public List<VirtualChannel> getList(){
        return service.getList();
    }

    @Transactional
    public Boolean saveFx(VirtualChannel virtual){
        VirtualChannel oldVirtual = service.findById(virtual.getId());
        String oldFx=oldVirtual.getFx();
        VirtualChannel v=service.saveFx(virtual);
        //修改virtual缓存
        saveVirtualCache(oldFx);
        saveVirtualCache(v.getFx());
        return true;
    }

    public void saveVirtualCache(String fx){
        if(!StringUtils.isEmpty(fx)){
            List<String> fxChannelList=channelProcessor.getFullCodesByFx(fx);
            if(!CollectionUtils.isEmpty(fxChannelList)){
                fxChannelList.forEach(p->{
//                    String[] strings=p.split("_");
                    virtualChannelCache.saveByDeviceChannel(p.substring(0,p.lastIndexOf("_")), p.substring(p.lastIndexOf("_")+1));
                });
            }
        }
    }

    @Transactional
    @Override
    public boolean removeById(String id) {
        VirtualChannel virtual=service.findById(id);
        String oldFx=virtual.getFx();

        channelService.removeByVirtualId(id);
        boolean result= super.removeById(id);
        saveVirtualCache(oldFx);
        return result;
    }
}